package cn.com.dhcc.turbo.service.transport;

import org.springframework.stereotype.Component;

import cn.com.dhcc.app.core.util.Status;
import cn.com.dhcc.turbo.entity.exch.QueueMsgWrapper.ExchageLifecycle;

/**
 * Http传输管道，通过HTTP form表单提交的方式将数据传输至目的节点
 *
 */
@Component
public class HttpTransportPipeline implements TransportPipeline {
//	private static Logger logger = Log.getExistLogger(LOGTYPE.R);
//	private static final String RECV_CMDATA_URI = "/exch/http_pipline/recv_cmdata.do";
//	private static final String RECV_SEND_STATUS_AND_ROUTELOG_URI = "/exch/http_pipline/recv_send_status_and_routelog.do";
//	private static final String RECV_NOTIFY_STATUS_AND_ROUTELOG_URI = "/exch/http_pipline/recv_notify_status_and_routelog.do";

/*	@Autowired
	@Qualifier(IocBean.BASE_INFO_SERVICEIMPL_BEAN)
	private BaseInfoService baseInfoService;*/

	@Override
	public Status sendData(Object data, String fromCode, String toCode,
			ExchageLifecycle exchageLifecycle) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Status sendLog(Object data, String fromCode, String toCode,
			ExchageLifecycle exchageLifecycle) {
		// TODO Auto-generated method stub
		return null;
	}

	/*@Override
	public Status sendData(Object data) {
		logger.info("HttpTransportPipeline >>>>>> sending data[" + data + "]");
		Status status = Status.getInstance();
		String msg = "";
		try {
			SendCmData cmData = (SendCmData) data;
			String filePath = AppConst.CMDATA_STORED_DIR + "/" + cmData.getPayload();
			File file = new File(filePath);
			if (!file.exists()) {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败：数据报文文件[" + filePath + "]找不到";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			String payloadContent = FileUtils.readFileToString(file);
			cmData.setPayloadContent(payloadContent);
			NodeInfo toNode = baseInfoService.getNodeInfoByCode(cmData.getRecvNodecode());
			if (toNode == null) {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:接收节点[" + cmData.getRecvNodecode() + "]信息查找失败";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			String url = toNode.getInstallUrl() + RECV_CMDATA_URI;
			if (StrUtil.isBlank(url)) {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:接收节点[" + cmData.getRecvNodecode() + "]installUrl地址未填写";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("data", JsonUtil.toJsonWithDateFormat(cmData));
			status = HttpClientUtil.post(url, paramMap);
			if (status.isSuccess()) {
				String respJsonStr = (String) status.getData();
				Status respStatus = JsonUtil.toObject(respJsonStr, Status.class);

				if (respStatus.isSuccess()) {
					msg = "数据发送至传输管道成功：[HttpTransportPipeline]";
					SendCmData cmdDataResp = JsonUtil.toObject(JsonUtil.toJson(respStatus.getData()), SendCmData.class);
					status.setSuccess(true);
					status.setMsg(msg);
					status.setData(cmdDataResp);
				} else {
					msg = "数据发送至传输管道成功[HttpTransportPipeline]，但对方回执失败:" + respStatus.getMsg();
					status.setSuccess(false);
					status.setMsg(msg);
				}
			} else {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:" + status.getMsg();
				status.setSuccess(false);
				status.setMsg(msg);
			}
			return status;
		} catch (Exception e) {
			msg = "数据发送至传输管道[HttpTransportPipeline]出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);

			logger.error(msg, e);
			return status;
		}
	}
	
	
	@Override
	public Status sendDataToOuterSendPipeline(Object data) {
		//un support
		return null;
	}

	@Override
	public Status sendSendStatusAndRouteLog(Object data) {
		logger.info("HttpTransportPipeline >>>>>> sending RouteLog[" + data + "]");
		Status status = Status.getInstance();
		String msg = "";
		try {
			SendCmData cmData = (SendCmData) data;
			NodeInfo fromNode = baseInfoService.getNodeInfoByCode(cmData.getSendNodecode());
			if (fromNode == null) {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:发送节点[" + cmData.getSendNodecode() + "]信息查找失败";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			String url = fromNode.getInstallUrl() + RECV_SEND_STATUS_AND_ROUTELOG_URI;
			if (StrUtil.isBlank(url)) {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:发送节点[" + cmData.getSendNodecode() + "]installUrl地址未填写";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}

			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("data", JsonUtil.toJsonWithDateFormat(cmData));
			status = HttpClientUtil.post(url, paramMap);
			if (status.isSuccess()) {
				String respJsonStr = (String) status.getData();
				Status respStatus = JsonUtil.toObject(respJsonStr, Status.class);

				if (respStatus.isSuccess()) {
					msg = "数据发送至传输管道成功：[HttpTransportPipeline]";
					status.setSuccess(true);
					status.setMsg(msg);
				} else {
					msg = "数据发送至传输管道成功[HttpTransportPipeline]，但对方回执失败:" + respStatus.getMsg();
					status.setSuccess(false);
					status.setMsg(msg);
				}
			} else {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:" + status.getMsg();
				status.setSuccess(false);
				status.setMsg(msg);
			}
			return status;
		} catch (Exception e) {
			msg = "数据发送至传输管道[HttpTransportPipeline]出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);

			logger.error(msg, e);
			return status;
		}
	}

	@Override
	public Status sendNofityStatusAndRoutLog(Object data) {
		logger.info("HttpTransportPipeline >>>>>> sending RouteLog[" + data + "]");
		Status status = Status.getInstance();
		String msg = "";
		try {
			SendCmData cmData = (SendCmData) data;
			NodeInfo toNode = baseInfoService.getNodeInfoByCode(cmData.getRecvNodecode());
			if (toNode == null) {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:接收节点[" + cmData.getRecvNodecode() + "]信息查找失败";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			String url = toNode.getInstallUrl() + RECV_NOTIFY_STATUS_AND_ROUTELOG_URI;
			if (StrUtil.isBlank(url)) {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:接收节点[" + cmData.getRecvNodecode() + "]installUrl地址未填写";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("data", JsonUtil.toJsonWithDateFormat(cmData));
			status = HttpClientUtil.post(url, paramMap);
			if (status.isSuccess()) {
				String respJsonStr = (String) status.getData();
				Status respStatus = JsonUtil.toObject(respJsonStr, Status.class);

				if (respStatus.isSuccess()) {
					msg = "数据发送至传输管道成功：[HttpTransportPipeline]";
					status.setSuccess(true);
					status.setMsg(msg);
				} else {
					msg = "数据发送至传输管道成功[HttpTransportPipeline]，但对方回执失败:" + respStatus.getMsg();
					status.setSuccess(false);
					status.setMsg(msg);
				}
			} else {
				msg = "数据发送至传输管道[HttpTransportPipeline]失败:" + status.getMsg();
				status.setSuccess(false);
				status.setMsg(msg);
			}
			return status;
		} catch (Exception e) {
			msg = "数据发送至传输管道[HttpTransportPipeline]出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);

			logger.error(msg, e);
			return status;
		}
	}
*/
/*
	@Override
	public Status sendSendStatusAndRouteLogToSendNode(Object data) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Status sendSendStatusAndRouteLogToInnerEntrepot(Object data) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Status sendNofityStatusAndRoutLogToOuterEntrepot(Object data) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Status sendDataToOuterSendPipeline(Object data,
			NodeInfo sendNodeInfo, NodeInfo toNodeInfo) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Status sendNofityStatusAndRoutLog(Object data, NodeInfo localNode) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Status sendData(Object data, String fromCode, String toCode,
			ExchageLifecycle exchageLifecycle) {
		// TODO Auto-generated method stub
		return null;
	}*/

}
